home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Timothy Knox / yerk 3.66 / System source / Proc < prev    next >
Text File  |  1994-06-24  |  4KB  |  107 lines

  1. \ A pair of words designed to enable Yerk words to be called with a
  2. \  Pascal lookalike stack. Useful when a routine is passed to a Tool-
  3. \  box entry for asynchronous routines.
  4. \  Terminology: Any such Pascal lookalike is called a proc word
  5. \ 10/15/84  RW
  6. \ 12/22/85  CBD Rewrote PSTART, (Hyperdrive fix)
  7. \  8/31/88    rfl    took out meaningless nop in pstart
  8. \  4/01/90    rfl    took out adda.l #$48,a3 since no longer have old yerk header
  9. \ 12/30/90    rfl stack now 3000, and 300 for method stack
  10. \  6/07/91    rfl yerk base now 2c from a5
  11. \ 10/26/91    rfl    increased stack to 7000 for use with system 7
  12. \  4/11/92    rfl    flush cache on :proc if necessary
  13. \  5/02/92    rfl    changed the way a5,a3 are recovered..stored at startup
  14. \                  in a resource called MYA5.
  15. \  4/24/93    rfl    once again, changed the way a5,a3 are recovered. The resource
  16. \                  method moves memory and you don't want that if it is
  17. \                  called by interrupt. So now, to be safe, all procwords
  18. \                  defined in your application will be inited with a5,a3 at
  19. \                  startup time by sticking 'initProcs' into your startup word.
  20. \ 5/18/93    rfl    protected initproc from an assembly proc
  21. \ 1/2/94    rfl    reset stacks to larger values in proc
  22. Hex
  23.  
  24. 0 value pstartLen
  25.  
  26. \ PSTART - Converts Pascal stack format to our Forth stack format.
  27. \            N.B. - VERY IMPORTANT!!! - This word will never be
  28. \            directly executed. Instead the code will be CMOVE'd
  29. \            into place during the execution of PROC and executed by
  30. \            the routine active via JSR.
  31.  
  32. Create Pstart <[
  33.     600c w,            \         bra.s        next        \ jump over data area
  34.     'type proc ,    \                                \ marker to identify it as proc
  35.     0      ,            \ data                            \ a5 will be here
  36.     0      ,            \                                 \ a3 will be here
  37.     204E w,            \ next    movea.l    a6,a0            \ store return stack ptr
  38.     2C4F w,            \     movea.l    a7,a6                \ save parm stack
  39.     9DFC w, 2328 ,    \     suba.l    #12000,a6            \ allow stack to have 12000 bytes
  40.     2D08 w,            \     move.l    a0,-(a6)            \ save old return stack ptr here
  41.     2D1F w,            \     move.l    (a7)+,-(a6)            \ save return address here
  42.     48E63F1C ,        \     movem.l    d2-d7/a3-a5,-(a6)    \ save these registers, including a5
  43.                                                     \  and a3
  44.     49faffe4 ,        \    lea        data(pc),a4            \ point to a5 data area
  45.     2a5c w,            \    movea.l    (a4)+,a5            \ get a5
  46.     2654 w,            \    movea.l    (a4),a3                \ get a3, ptr to yerk base
  47.  
  48.     2A0E w,            \    move.l    a6,d5                \ let ret stack have only 700
  49.     0485 w, 1f4 ,    \    subi.l    #700,d5                \ and give method stack the rest
  50.     49FA0006 ,        \    lea        6(pc),a4               \ load a4 with ptr to routine
  51. next,
  52.  
  53. \ PEXIT - This code is equally tricky as the above PSTART. This
  54. \          restores the old A6 register and then jumps back to the
  55. \          return location from which the word was called. This
  56. \          code word will be invoked through the colon code, but
  57. \          colon-code will never see it again.
  58. Create P;s <[
  59.     4CDE38FC ,        \ movem.l    (a6)+,d2-d7/a3-a5    \ restore a3 and a5 especially
  60.     205E w,            \ movea.l    (a6)+,a0
  61.     2C5E w,            \ movea.l    (a6)+,a6
  62.     4ED0 w,            \ jmp        (a0)
  63.  
  64. Decimal
  65. ' P;s nfa ' Pstart -  docs 2* -  -> pstartLen    \ if documentation on, subtract 2.
  66.  
  67. \ build a word that looks like a Pascal procedure at its PFA
  68. : :PROC
  69.     ?exec create    \  build hdr, cfa
  70.     ' pstart here pstartLen allot   pstartLen cMove
  71.     cflush    \ flush caches on appropriate machines
  72.     ]> ;    \ enter compilation state
  73.     
  74. : ;PROC   Compile P;s  [Compile] <[   ;  Immediate
  75.  
  76. \ don't assume proc word is always a :proc def..could be assembly
  77. : initProc ( 'cproc -- ) >body dup 2+ @ $ 70726f63 =        \ check for 'proc'
  78.     IF 6 + geta3a5 rot swap over ! 4+ ! ELSE drop THEN ;
  79.  
  80. : (initProcs) { theCfa arg -- } theCfa 6 + @ 'type proc =    \ check for marker
  81.     IF theCfa initProc ." initProc: " theCfa >name id. cr THEN ;            \ it's a procword, so init it
  82.  
  83. \ This word will initialize each procword in your program (at startup time)
  84. : initProcs 'c (initProcs) 0 trav ;
  85.  
  86. \ **** STACK LAYOUT DURING PROCEDURE
  87. \               |
  88. \ method stack  |
  89. \               | <---  d5
  90. \ ______________|
  91. \               |
  92. \ return stack  | <---  a6
  93. \ ______________|
  94. \               |
  95. \ data stack    | <---  A7'  = A7+4 (NEW DATA STACK)
  96. \               | <---- a7
  97. \               |
  98. \               |
  99. \ A6            |
  100. \ (A7) RETURN   |
  101. \ D,A REGISTERS | <---  A6'  = A7-3000 (NEW RETURN STACK)
  102. \               |
  103. \               |
  104. \               | <---  D5'  = A6'-300 (NEW METHODS STACK)
  105.  
  106.